package kuangsheng;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServiceDeom02 {

    public static void main(String[] args) throws IOException {
        //创建服务
        ServerSocket serverSocket = new ServerSocket(9999);
        //监听客户端连接
        //阻塞监听，会一直等待客户端连接
        Socket accept = serverSocket.accept();
        //获取输入流
        InputStream inputStream = accept.getInputStream();
        //文件输出
        FileOutputStream fileOutputStream = new FileOutputStream(new File("test1.jpg"));
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, len);
        }

        //通知客户端接收完毕
        OutputStream outputStream = accept.getOutputStream();
        outputStream.write("接收完毕了！你可以断开了!".getBytes());


        //关闭资源
        fileOutputStream.close();
        inputStream.close();
        accept.close();
        serverSocket.close();
    }

}
